package com.atstudent.gmall.web.controller;

import com.atstudent.gmall.cart.vo.AddCartSuccessVo;
import com.atstudent.gmall.common.feign.cart.CartFeignClient;
import com.atstudent.gmall.common.feign.util.HttpServletRequestThreadLocal;
import com.atstudent.gmall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 薛有朋
 * @CreatDate 2025/4/7 19:00
 **/
@Controller
@Slf4j
public class CartController {
    
    @Autowired
    private CartFeignClient cartFeignClient;

    /**
     * 添加商品到购物车
     * @Author 薛有朋
     * @Date 2025/4/18 20:49
     **/
    @GetMapping(value = "/addCart.html")
    public String addCart(@RequestParam(value = "skuId") Long skuId ,
                          @RequestParam(value = "skuNum") Integer skuNum ,
                          Model model){
        AddCartSuccessVo addCartSuccessVo = cartFeignClient.addCart(skuId, skuNum).getData();

        if (addCartSuccessVo == null){
            return "cart/addCartError";
        }

        model.addAttribute("skuInfo" , addCartSuccessVo.getSkuInfo());
        model.addAttribute("skuNum" , addCartSuccessVo.getSkuNum());
        return "cart/addCart";
    }

    /**
     * 跳转到购物车页面
     * @Author 薛有朋
     * @Date 2025/4/18 20:49
     **/
    @GetMapping(value = "/cart.html")
    public String cart(){
        return "cart/index";
    }


    /**
     * 点击删除选中的商品 跳转到购物车页面
     * @Author 薛有朋
     * @Date 2025/4/21 20:16
     **/
    @GetMapping(value = "/cart/deleteChecked")
    public String deleteChecked(){
        cartFeignClient.deleteAllChecked();
        return "cart/index";
    }
}
